# -*- coding: utf-8 -*-
import src

import uvicorn
import aiosqlite

from fastapi    import FastAPI
from contextlib import asynccontextmanager

from fastapi.templating  import Jinja2Templates
from fastapi.staticfiles import StaticFiles


@asynccontextmanager
async def on_startup(application: FastAPI):
    application.database = await aiosqlite.connect(
        "./stack/database.db")
    await src.utils.check.database(application.database)

    yield

    await application.database.close()


app = FastAPI(
    title    = "История Погоды",
    version  = "0.0.1",
    lifespan = on_startup)


app.mount("/static", StaticFiles(directory = "./stack/static"), name = "static")
app.template = Jinja2Templates(directory = "./stack/templates")


for route in src.backend_routes():
    app.include_router(route)

for route in src.frontend_routers():
    app.include_router(route)


if __name__ == "__main__":
    uvicorn.run("app:app",
                host = "127.0.0.1",
                port = 80, reload = True)